﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.IO.SensorServices.Rfid.Client;

namespace Securfid
{
    class Lectures
    {
        //Variables de la classe Lectures
        private int id_cegep;
        public string id_rfid;
        private string emplacement;
        private string time;
        private string heure_alarme;
        public bool alarme {get; set;}
        private int poste;
        private int id_appareil;

        //Intialisation
        public void Initialise(int C, string R, string E, string T, string HA, bool A, int P, int IDA)
        {
            this.id_cegep = C;
            this.id_rfid = R;
            this.emplacement = E;
            this.time = T;
            this.heure_alarme = HA;
            this.alarme = A;
            this.poste = P;
            this.id_appareil = IDA;
        }

        //Accesseurs
        public int Getid_cegep()
        {
            return id_cegep;
        }
        public String Getid_rfid()
        {
            return id_rfid;
        }
        public String Getemplacement()
        {
            return emplacement;
        }
        public String Gettime()
        {
            return time;
        }
        public String GetHeureAlarme()
        {
            return heure_alarme;
        }
        public bool GetAlarme()
        {
            return alarme;
        }
        public int GetPoste()
        {
            return poste;
        }
        public int GetIdAppareil()
        {
            return id_appareil;
        }

        public static List<Lectures> Alarme(List<Lectures> TagSQL, int temps_alarme, DateTime heureactuelle)
        {
            List<Lectures> alarme = new List<Lectures>();

            foreach (Lectures tag in TagSQL)
            {
                DateTime tagtime = Convert.ToDateTime(tag.Gettime());
                System.TimeSpan timediff = heureactuelle - tagtime;

                if (timediff.TotalSeconds > temps_alarme)
                {
                    alarme.Add(tag);
                    Console.WriteLine("Alarme sur le tag : {0}", tag.Getid_rfid());
                }
            }
            return alarme;
        }

        public static void ActiveAlarme(List<Lectures> listeTagSQL, string devicename, bool stop_alarme)
        {
            //Alarme ON = 0 / OFF = 1
            byte[] etat = new byte[] { 1 };

            if (stop_alarme == true)
            {
                foreach (Lectures list in listeTagSQL)
                {
                    if (list.alarme == true)
                    {
                        etat[0] = 0;
                    }
                }

                using (DeviceConnection connection = new DeviceConnection(devicename))
                {
                    connection.Open();
                    connection.SetProperty("GPO_1", Microsoft.SensorServices.Rfid.Dspi.StandardDevicePropertyGroups.Source, Microsoft.SensorServices.Rfid.Dspi.SourcePropertyGroup.PortOutputValue, etat, null);
                    connection.Close();
                }
            }
            else
            {
                using (DeviceConnection connection = new DeviceConnection(devicename))
                {
                    connection.Open();
                    connection.SetProperty("GPO_1", Microsoft.SensorServices.Rfid.Dspi.StandardDevicePropertyGroups.Source, Microsoft.SensorServices.Rfid.Dspi.SourcePropertyGroup.PortOutputValue, etat, null);
                    connection.Close();
                }
            }
        }
    }
}